/*
 *    Document   : Diarias
 *    Created on : 20/02/2013, 11:43:59
 *    Author     : Fiúza <rbfiuza@gmail.com>
 */


Ext.define('FC.controller.Diarias', {
    extend: 'Ext.app.Controller',

    stores:['Diarias'],
    models:['Diaria'],
    views:['DiariaList'],
   
    init : function() {
        this.control({
                    
            'viewport' : {
                render : this.onPanelRendered
            },
            'diariaList': {
                edit: this.editDiaria
                
            },

            'checkcolumn': {
                checkchange: this.checkboxChanged
            }
        });
    },
 
    onPanelRendered : function() {
        //just a console log to show when the panel is rendered
        Ext.MessageBox.alert('Status', 'Error occured during update Diaria');
        
    },
            
    editDiaria : function(editor, obj) {
        //check if record is dirty
        if(obj.record.dirty){
            //check if the record is valid   
//            console.log(obj.record.validate());
            if(obj.record.validate().isValid()){
                //Make your Ajax request to sync data
                mode = (obj.record.get('userId') === "") ? 'insert': 'update';
                this.syncData(obj, mode);
            }
        }
    },
            
    checkboxChanged : function(column,rowIndex,checked) {
        console.log('Checkbox changed');
        //grid column information
        console.log(column);
        //grid row number
        console.log(rowIndex);
        //the checkbox value
        console.log(checked);
        console.log(this.getUsersStore().getAt(rowIndex));
        //Make your Ajax request to sync data
        this.syncData(rowIndex,'update');
    },
            
    //Sync data with the server 
    syncData : function(obj, mode) {
        Ext.Ajax.request({
            url: 'atualizaDiaria',
            params: {
                store_id: 1,
                action: mode,
                rowIndex: obj.rowIdx,
                recordInfo: Ext.encode(this.getDiariasStore().getAt(obj.rowIdx).data)
            },
            scope:this,
            //method to call when the request is successful
            success: this.onSaveSuccess,
            //method to call when the request is a failure
            failure: this.onSaveFailure
        });
    },

    onSaveFailure: function(err){
        //Alert the user about communication error
        Ext.MessageBox.alert('Status', 'Error occured during update Diaria');
    },
    
    onSaveSuccess: function(response,opts){
        //Remove dirty
        var ajaxResponse = Ext.decode(response.responseText);
        if(ajaxResponse.success){
            //if we are doing an insert then get the new customerId 
            //and update the store record
            if(opts.params.action === 'insert'){
                diariaId = ajaxResponse.diariaId;
                this.getDiariasStore().getAt(opts.params.rowIndex).set('customerId',diariaId);
            }

//            this.getDiariasStore().getAt(opts.params.rowIndex).set('id',ajaxResponse.diarias.polos);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('diaria',ajaxResponse.diarias.diaria);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('encontros',ajaxResponse.diarias.encontros);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('semestres',ajaxResponse.diarias.semestres);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('pessoas',ajaxResponse.diarias.pessoas);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('encontros_polo',ajaxResponse.diarias.encontros_polo);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('diarias_encontro',ajaxResponse.diarias.diarias_encontro);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('polos',ajaxResponse.diarias.polos);            
            this.getDiariasStore().getAt(opts.params.rowIndex).set('diarias_aprovadas',ajaxResponse.diarias.diarias_aprovadas);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('valor_unitario',ajaxResponse.diarias.valor_unitario);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('valor_final',ajaxResponse.diarias.valor_final);
            this.getDiariasStore().getAt(opts.params.rowIndex).set('indicacoes_parametros',ajaxResponse.diarias.indicacoes_parametros);
            this.getDiariasStore().getAt(opts.params.rowIndex).commit();
//            this.getDiariasStore().save();

        }
        else {
            Ext.MessageBox.alert('Status', 'Error occured during update Diaria');
        }


//      e.record.set(e.field,gridValue);
//      e.record.commit();
//      myGrid.getView().refresh();
    

    
    }
});